#include "musicinstructionview.h"
#include "ui_musicinstructionview.h"
#include "../../programView/programview.h"

MusicInstructionView::MusicInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    musicFileList.clear();

    QStringList strHeader;
    strHeader << tr("音乐文件");
    ui->TabView_MusicFile->setHorizontalHeaderLabels(strHeader);
//        ui->TabView_MusicFile->setHorizontalHeaderLabels(strList);
    ui->TabView_MusicFile->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
    ui->TabView_MusicFile->setColumnCount(1);
    ui->TabView_MusicFile->setColumnWidth(0, 440);

//    musicFileModel = new QStandardItemModel;
//    musicFileModel-(0,new QStandardItem(QObject::tr("音乐文件名")));
//    ui->TabView_MusicFile->setModel(musicFileModel);

//    // 设置表头宽度填充整个控件
//    ui->TabView_MusicFile->horizontalHeader()->setStretchLastSection(true);
//    // 设置表头居中
//    ui->TabView_MusicFile->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);

//    // 设置不可编辑
//    ui->TabView_MusicFile->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->Edit_MusicVolume->setValidator(new QIntValidator(0,100,this));
    ui->Slider_MusicVolume->setMaximum(100);
    ui->Slider_MusicVolume->setMinimum(0);

}

void MusicInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->TabView_MusicFile->setGeometry(10,10,430,150);
        ui->MusicSettins_GBox->setGeometry(10,165,430,110);
        ui->note_GBox->setGeometry(10,280,430,70);

        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void MusicInstructionView::getInput()
{
    // 获取选中行的文本内容
    QModelIndex index = ui->TabView_MusicFile->currentIndex();
    if(index.row() < 0)
    {
        return;
    }
    musicFileName = musicFileList.at(index.row());
    qDebug() << "Current Selete FileName :" << musicFileName;

    musicVolume = ui->Edit_MusicVolume->text().toInt();
    if( ui->ComBox_MusicTimes->currentText() == tr("循环") )
    {
        musicPlayTimes = -1;
    }
    else if(ui->ComBox_MusicTimes->currentText() == tr("暂停"))
    {
        musicPlayTimes = 0;
    }
    else
    {
        musicPlayTimes = ui->ComBox_MusicTimes->currentText().toInt();
    }
    noteStr = ui->note_lineEdit->text();
}

void MusicInstructionView::updateView()
{
    ui->Slider_MusicVolume->setValue(musicVolume);
    ui->Edit_MusicVolume->setText(QString::number(musicVolume));
    if(musicPlayTimes == -1)
    {
        // 循环
        ui->ComBox_MusicTimes->setCurrentIndex(7);
    }
    else if(musicPlayTimes == 0)
    {
        // 暂停
        ui->ComBox_MusicTimes->setCurrentIndex(0);
    }
    else
    {
        // 1-6
        ui->ComBox_MusicTimes->setCurrentIndex(musicPlayTimes);
    }
    ui->note_lineEdit->setText(noteStr);
}

void MusicInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/musicInstructionView.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

int MusicInstructionView::initialMusicList()
{
    ui->TabView_MusicFile->clear();
    ui->TabView_MusicFile->setRowCount(musicFileList.count());
    QStringList strHeader;
    strHeader << tr("音乐文件");
    ui->TabView_MusicFile->setHorizontalHeaderLabels(strHeader);

    // 将音乐文件列表显示
    for(int i = 0; i < musicFileList.count(); i++)
    {
        ui->TabView_MusicFile->setItem(i,0, new QTableWidgetItem(musicFileList.at(i)));
    }

    // 更新显示音乐文件
    // 从容器中查找文件索引
    qDebug() << "Current File Row = " << musicFileName << musicFileList.indexOf(musicFileName,0);
    ui->TabView_MusicFile->selectRow(musicFileList.indexOf(musicFileName,0));
    ui->TabView_MusicFile->setFocus();

    update();
    return 1;
}

void MusicInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote();
}

void MusicInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionOut);   
}

void MusicInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void MusicInstructionView::on_Slider_MusicVolume_sliderMoved(int position)
{
    ui->Edit_MusicVolume->setText(QString::number(position));
}

void MusicInstructionView::on_Slider_MusicVolume_valueChanged(int value)
{
    ui->Edit_MusicVolume->setText(QString::number(value));
}

void MusicInstructionView::on_Edit_MusicVolume_textEdited(const QString &arg1)
{
    ui->Slider_MusicVolume->setSliderPosition(arg1.toInt());
}

void MusicInstructionView::listMusicDirSlot(const QHash<QString, bool> &musicDirInfoList, const QString &ftpPath, const QString &clientPath)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_MUSIC)
    {
        return;
    }
    else
    {
        musicFileList.clear();

        for(int i = 0; i < musicDirInfoList.keys().count(); i++)
        {
            if(!musicDirInfoList.values().at(i))
            {
                musicFileList << musicDirInfoList.keys().at(i);
            }
        }
        ui->TabView_MusicFile->clear();
        ui->TabView_MusicFile->setRowCount(musicFileList.count());
        QStringList strHeader;
        strHeader << tr("音乐文件");
        ui->TabView_MusicFile->setHorizontalHeaderLabels(strHeader);

        // 将音乐文件列表显示
        for(int i = 0; i < musicFileList.count(); i++)
        {
            ui->TabView_MusicFile->setItem(i,0, new QTableWidgetItem(musicFileList.at(i)));
        }

        // 更新显示音乐文件
        // 从容器中查找文件索引
        qDebug() << "Current File Row = " << musicFileName << musicFileList.indexOf(musicFileName,0);
        ui->TabView_MusicFile->selectRow(musicFileList.indexOf(musicFileName,0));
        ui->TabView_MusicFile->setFocus();
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
    }
    update();
}

//有FTP端编码转本地编码
QString MusicInstructionView::_FromSpecialEncoding(const QString &InputStr)
{
#ifdef Q_OS_WIN
    return  QString::fromLocal8Bit(InputStr.toLatin1());
#else
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    if (codec)
    {
        return codec->toUnicode(InputStr.toLatin1());
    }
    else
    {
        return QString("");
    }
#endif
}
<<<<<<< HEAD
=======

int MusicInstructionView::setMusicFileList(QVector<QString> musicFileListIn)
{
    musicFileList=musicFileListIn;
    initialMusicList();
    return 1;
}
>>>>>>> dev
